<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <van-loading color="#0094ff" v-if='!articleDetail.title' vertical class="mt">
      疯狂加载中……
    </van-loading>
    <div v-else>
      <!-- 文章信息区域 -->
      <div class="article-container">
        <!-- 文章标题 -->
        <h1 class="art-title">{{ articleDetail.title }}</h1>

        <!-- 用户信息 -->
        <van-cell
          center
          :title="articleDetail.aut_name"
          :label="articleDetail.pubdate"
        >
          <template #icon>
            <img :src="articleDetail.aut_photo" alt="" class="avatar" />
          </template>
          <template #default>
            <div>
              <van-button
                type="info"
                size="mini"
                v-if="articleDetail.is_followed"
                @click="followFn(1)"
                >已关注</van-button
              >
              <van-button
                icon="plus"
                type="info"
                size="mini"
                plain
                v-else
                @click="followFn(2)"
                >关注</van-button
              >
            </div>
          </template>
        </van-cell>

        <!-- 分割线 -->
        <van-divider></van-divider>

        <!-- 文章内容 -->
        <div
          class="art-content"
          v-html="articleDetail.content"
        ></div>

        <!-- 分割线 -->
        <van-divider>End</van-divider>

        <!-- 点赞 -->
        <div class="like-box">
          <van-button
            icon="good-job"
            type="danger"
            size="small"
            v-if="articleDetail.attitude === 1"
            @click="likeArticl(1)"
            >已点赞</van-button
          >
          <van-button
            icon="good-job-o"
            type="danger"
            plain
            size="small"
            v-else
            @click="likeArticl(2)"
            >点赞</van-button
          >
        </div>
      </div>
      <CommentList></CommentList>
    </div>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
import CommentList from './CommentList.vue'
import {
  getArticleDetailAPI,
  followAuthorAPI,
  unFollowAuthorAPI,
  likeArticleAPI,
  unLikeArticleAPI
} from '@/api'
export default {
  name: 'articleDetail',
  components: {
    CommentList
  },
  data () {
    return {
      articleDetail: {}
    }
  },
  created () {
    this.getArticleDetailFn()
    this.setArtIdMutations(this.$route.query.artId)
  },
  methods: {
    ...mapMutations('reply', ['setArtIdMutations']),
    async getArticleDetailFn () {
      const res = await getArticleDetailAPI(this.$route.query.artId)
      this.articleDetail = res.data.data
    },
    // 关注作者
    async followFn (key) {
      const autId = this.articleDetail.aut_id
      if (key === 1) {
        await unFollowAuthorAPI(autId)
      } else {
        await followAuthorAPI(autId)
      }
      // this.articleDetail.is_followed = !this.articleDetail.is_followed
      this.getArticleDetailFn()
    },
    async likeArticl (key) {
      const artId = this.articleDetail.art_id
      if (key === 1) {
        await unLikeArticleAPI(artId)
      } else {
        await likeArticleAPI(artId)
      }
      this.getArticleDetailFn()
    }
  }
}
</script>

<style scoped lang="less">
.mt{
  position: fixed;
  top: 50%;
  left: 50%;
  margin-left: -46px;
  margin-top: -28px;
}
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  ::v-deep img {
    width: 100%;
  }
  ::v-deep pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
